import { defineStore } from "pinia";
import { v4 as uuidv4 } from "uuid";
// let a = uuidv4(5);
interface Return {
  id: string;
  videoState: boolean;
  rtspArr: RtspArr[];
}
export interface RtspArr {
  id: string;
  name: string;
  rtsp: string;
}
export const mainStore = defineStore("main", {
  // 存放数据
  state: (): Return => {
    return {
      id: "1",
      videoState: false,
      rtspArr: [
        {
          id: "1",
          name: "小动物",
          rtsp: "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4",
        },
        {
          id: "2",
          name: "小动物2",
          rtsp: "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4",
        },
      ],
    };
  },
  // 修改数据（类似计算属性）
  getters: {
    getRtsp() {
      let arr: RtspArr[] = this.rtspArr.filter(
        (item: RtspArr) => this.id === item.id
      );
      return arr[0];
    },
  },
  // 方法
  actions: {
    onState(id: string) {
      this.videoState = false;
      this.id = id;
      setTimeout(() => {
        this.videoState = true;
      }, 100);
    },
    onStop(id: string) {
      this.videoState = false;
    },
    onAdd(info: RtspArr) {
      info.id = uuidv4();
      this.rtspArr.push(info);
    },
    onEdit(info: RtspArr) {
      // this.rtspArr.forEach((item) => {
      //   if (item.id === info.id) {
      //     console.log(item,info);
      //     item = info;
      //   }
      // });
      // this.rtspArr.filter((item) => {
      //   if (item.id === info.id) {
      //     console.log(item,info);
      //     return info;
      //   } else {
      //     return item;
      //   }
      // });

      console.log(this.rtspArr);
    },
    onDelete(id: string) {
      console.log(id);
      this.rtspArr.filter((item) => item.id !== id);
    },
  },
});
